<template>
  <div class="table-page">
    <GiTable row-key="id" title="微信名片管理" :data="dataList" :columns="columns" :loading="loading"
      :scroll="{ x: 2000, y: '100%', minWidth: 1000 }" :pagination="pagination" :disabled-tools="['size']"
      :disabled-column-keys="['name']" @refresh="search">
      <template #toolbar-left>
        <a-input v-model="queryForm.name" placeholder="请输入姓名" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #toolbar-right>
        <a-button v-permission="['card:businessCard:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['card:businessCard:list']" title="查看" @click="onDetail(record)">查看</a-link>
          <a-link v-permission="['card:businessCard:update']" title="修改" @click="onUpdate(record)">修改</a-link>
          <a-link title="更多配置" @click="onMore(record)">更多配置</a-link>
          <a-link v-permission="['card:businessCard:delete']" status="danger" :disabled="record.disabled" title="删除"
            @click="onDelete(record)">
            删除
          </a-link>
        </a-space>
      </template>
      <template #gender="{ record }">
        <GiCellGender :gender="record.gender" />
      </template>
      <template #status="{ record }">
        <GiCellStatus :status="record.status" />
      </template>
      <template #name="{ record }">
        <GiCellAvatar :avatar="record.avatar" :name="record.name" />
      </template>
    </GiTable>

    <BusinessCardAddModal ref="BusinessCardAddModalRef" @save-success="search" />
    <BusinessCardDetailDrawer ref="BusinessCardDetailDrawerRef" />
    <BusinessCardMore ref="BusinessCardMoreRef" />
  </div>
</template>

<script setup lang="ts">
import { deleteBusinessCard, listBusinessCard, type BusinessCardQuery, type BusinessCardResp } from '@/apis/website'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'
import BusinessCardAddModal from './BusinessCardAddModal.vue'
import BusinessCardDetailDrawer from './BusinessCardDetailDrawer.vue'
import BusinessCardMore from './more.vue'

defineOptions({ name: 'BusinessCard' })


const queryForm = reactive<BusinessCardQuery>({
  name: undefined,
  gender: undefined,
  status: undefined,
  createUser: undefined,
  createTime: undefined,
  sort: ['createTime,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listBusinessCard({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstanceColumns[] = [
  {
    title: '序号',
    width: 66,
    align: 'center',
    fixed: !isMobile() ? 'left' : undefined,
    render: ({ rowIndex }) => h('span', {}, rowIndex + 1 + (pagination.current - 1) * pagination.pageSize),
  },
  // { title: 'ID', dataIndex: 'id', slotName: 'id' },
  {
    title: '姓名', dataIndex: 'name', slotName: 'name', width: 120,
    ellipsis: true,
    tooltip: true,
    fixed: !isMobile() ? 'left' : undefined,
  },
  { title: '性别', dataIndex: 'gender', slotName: 'gender', width: 100 },
  { title: '微信号', dataIndex: 'wechat', slotName: 'wechat' },
  // { title: '个人简介', dataIndex: 'introduction', slotName: 'introduction', ellipsis: true, tooltip: true },
  { title: '手机号码', dataIndex: 'phone', slotName: 'phone', width: 160 },
  { title: '邮箱', dataIndex: 'email', slotName: 'email', width: 220 },
  { title: '公司', dataIndex: 'companyName', slotName: 'companyName', width: 260 },
  { title: '职位', dataIndex: 'title', slotName: 'title' },
  // { title: '地址', dataIndex: 'address', slotName: 'address', ellipsis: true, tooltip: true },
  { title: '状态', dataIndex: 'status', slotName: 'status', width: 100 },
  { title: '描述', dataIndex: 'description', slotName: 'description', ellipsis: true, tooltip: true },
  // { title: '创建人', dataIndex: 'createUser', slotName: 'createUser' },
  // { title: '创建时间', dataIndex: 'createTime', slotName: 'createTime' },
  // { title: '修改人', dataIndex: 'updateUser', slotName: 'updateUser' },
  // { title: '修改时间', dataIndex: 'updateTime', slotName: 'updateTime' },
  {
    title: '操作',
    slotName: 'action',
    width: 240,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['card:businessCard:update', 'card:businessCard:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.name = undefined
  queryForm.gender = undefined
  queryForm.status = undefined
  queryForm.createUser = undefined
  queryForm.createTime = undefined
  search()
}

// 删除
const onDelete = (record: BusinessCardResp) => {
  return handleDelete(() => deleteBusinessCard(record.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

const BusinessCardAddModalRef = ref<InstanceType<typeof BusinessCardAddModal>>()
const BusinessCardAddRef = ref<InstanceType<typeof BusinessCardAdd>>()
// 新增
const onAdd = () => {
  BusinessCardAddModalRef.value?.onAdd()
  //BusinessCardAddRef.value?.onAdd()
}

// 修改
const onUpdate = (record: BusinessCardResp) => {
  BusinessCardAddModalRef.value?.onUpdate(record.id)
}

const BusinessCardMoreRef = ref<InstanceType<typeof BusinessCardMore>>()
//更多配置
const onMore = (record: BusinessCardResp) => {
  BusinessCardMoreRef.value?.onUpdate(record.id)
}

const BusinessCardDetailDrawerRef = ref<InstanceType<typeof BusinessCardDetailDrawer>>()
// 详情
const onDetail = (record: BusinessCardResp) => {
  BusinessCardDetailDrawerRef.value?.onDetail(record.id)
}
</script>

<style lang="scss" scoped></style>
